package model

import (
	"database/sql"
	"mall/pkg/orm"
	"time"
)

// 商城配置表
type Config struct {
	SgId              uint32       `gorm:"comment:品牌ID;primaryKey;type:integer"`
	OperationMode     uint8        `gorm:"comment:运营模式：0~自营，1~平台，2~经销商;type:smallint"`
	AllowBooking      bool         `gorm:"comment:是否允许预约，false~不允许，true~允许"`
	BookingDays       uint32       `gorm:"comment:最多提前x天预约;type:integer"`
	AutoConfirmedDays uint32       `gorm:"comment:发货后自动触发签收天数;type:integer"`
	RefundAllowedDays uint32       `gorm:"comment:签收后可发起售后天数;type:integer"`
	InventoryMode     uint8        `gorm:"comment:发货模式：0~品牌仓发货，1~店铺仓发货，2~部分店铺仓自行发货"`
	StoreIds          orm.IntArray `gorm:"comment:自行发货店铺列表;type:integer[]"`
	Operator          uint32       `gorm:"comment:操作人ID;type:integer"`
	CreatedAt         time.Time    `gorm:"comment:创建时间"`
	UpdatedAt         time.Time    `gorm:"comment:更新时间"`
}

type Decoration struct {
	SgId       uint32       `gorm:"comment:品牌ID;primaryKey;type:integer"`
	PStoreId   uint32       `gorm:"comment:总部&门店ID;primaryKey;type:integer"`
	SourceType uint8        `gorm:"comment:平台ID;primaryKey;type:smallint"`
	Decoration orm.JSON     `gorm:"comment:装修大字段"`
	ProdIds    orm.IntArray `gorm:"comment:货架商品ID序列;type:integer[]"`
	Operator   uint32       `gorm:"comment:操作人ID;type:integer"`
	CreatedAt  time.Time    `gorm:"comment:创建时间"`
	UpdatedAt  time.Time    `gorm:"comment:更新时间"`
}

type ShelfProd struct {
	ProdId           uint64          `gorm:"comment:商品ID;primaryKey;type:bigint"`
	PStoreId         uint32          `gorm:"comment:货架ID(总部/门店ID);primaryKey;type:integer;index:IDX_MALL_SHELF_PROD_SG,priority:2"`
	SgId             uint32          `gorm:"comment:品牌ID;type:integer;index:IDX_MALL_SHELF_PROD_SG,priority:1"`
	FromMenu         bool            `gorm:"comment:是否来自供货组;type:bool;"`
	CustomName       string          `gorm:"comment:货架自定义名称;size:100"`
	SyncName         string          `gorm:"comment:商品名称(需同步);size:100"`
	SyncTypeId       uint32          `gorm:"comment:商品二级分类ID(需同步);type:integer"`
	SyncNature       uint8           `gorm:"comment:商品类型;type:smallint"`
	SyncPic          string          `gorm:"comment:商品首图(需同步);size:100"`
	SyncUnit         string          `gorm:"comment:商品单位(需同步);size:32"`
	SyncGroups       orm.StringArray `gorm:"comment:商品分组信息;type:varchar(10)[]"`
	SyncGroupDetails orm.JSON        `gorm:"comment:商品分组明细"`
	SyncTime         time.Time       `gorm:"comment:商品同步时间"`
	ShelfStatus      bool            `gorm:"comment:上下架状态"`
	Operator         uint32          `gorm:"comment:操作人ID;type:integer"`
	CreatedAt        time.Time       `gorm:"comment:创建时间"`
	UpdatedAt        time.Time       `gorm:"comment:更新时间"`
}

type ShelfSku struct {
	SkuId        uint64          `gorm:"comment:规格ID;primaryKey;type:bigint"`
	PStoreId     uint32          `gorm:"comment:总部&门店ID;primaryKey;type:integer;index:IDX_MALL_SHELF_SKU_SG,priority:2;index:IDX_MALL_SHELF_SKU_DELETE,priority:2"`
	ProdId       uint64          `gorm:"comment:商品ID;type:bigint;index:IDX_MALL_SHELF_SKU_DELETE,priority:1"`
	SgId         uint32          `gorm:"comment:品牌ID;type:integer;index:IDX_MALL_SHELF_SKU_SG,priority:1"`
	CustomPrice  uint64          `gorm:"comment:货架自定义价格;type:bigint"`
	IsCustomized bool            `gorm:"comment:价格是否开启自定义"`
	SyncItems    orm.StringArray `gorm:"comment:规格属性项(需同步);varchar(50)[]"`
	SyncPrice    uint64          `gorm:"comment:供货组（经销商模式）/商品库价格(需同步);type:bigint"`
	SyncWeight   uint64          `gorm:"comment:商品规格重量(需同步);type:bigint"`
	SyncTime     time.Time       `gorm:"comment:商品同步时间"`
	Operator     uint32          `gorm:"comment:操作人ID;type:integer"`
	CreatedAt    time.Time       `gorm:"comment:创建时间"`
	UpdatedAt    time.Time       `gorm:"comment:更新时间"`
}

// 商品分组
type ShelfGroup struct {
	ID        uint32       `gorm:"comment:分组ID;type:serial;primaryKey"`
	Name      string       `gorm:"comment:分组名称;size:32;uniqueIndex:IDX_MALL_SHELF_GROUP_NAME,priority:3"`
	Code      int          `gorm:"comment:分组序号;type:integer"`
	SgId      uint32       `gorm:"comment:品牌ID;type:integer;index:IDX_MALL_SHELF_GROUP_SG,priority:1;uniqueIndex:IDX_MALL_SHELF_GROUP_NAME,priority:1"`
	PStoreId  uint32       `gorm:"comment:总部&门店ID;type:integer;index:IDX_MALL_SHELF_GROUP_SG,priority:2;uniqueIndex:IDX_MALL_SHELF_GROUP_NAME,priority:2"`
	Status    sql.NullBool `gorm:"comment:存在状态;index:IDX_MALL_SHELF_GROUP_SG,priority:3;uniqueIndex:IDX_MALL_SHELF_GROUP_NAME,priority:4"`
	Operator  uint32       `gorm:"comment:操作人ID;type:integer"`
	CreatedAt time.Time    `gorm:"comment:创建时间"`
	UpdatedAt time.Time    `gorm:"comment:更新时间"`
}

type ShelfGroupRef struct {
	GroupId   uint32    `gorm:"comment:分组ID;type:integer;primaryKey"`
	ProdId    uint64    `gorm:"comment:商品ID;type:bigint;primaryKey;index:IDX_MALL_SHELF_GROUP_REF_DELETE,priority:1"`
	Operator  uint32    `gorm:"comment:操作人ID;type:integer"`
	SgId      uint32    `gorm:"comment:品牌ID;type:integer;index:IDX_MALL_SHELF_GROUP_REF_SG,priority:1"`
	PStoreId  uint32    `gorm:"comment:总部&门店ID;type:integer;index:IDX_MALL_SHELF_GROUP_REF_SG,priority:2;index:IDX_MALL_SHELF_GROUP_REF_DELETE,priority:2"`
	UpdatedAt time.Time `gorm:"comment:更新时间"`
}

type Cart struct {
	MemberId  uint64    `gorm:"comment:会员ID;type:bigint;primaryKey;index:IDX_MALL_CART_MEMBER"`
	SkuId     uint64    `gorm:"comment:规格ID;primaryKey;type:bigint"`
	StoreId   uint32    `gorm:"comment:商品从属总部/门店ID;type:integer"`
	ProdId    uint64    `gorm:"comment:商品ID;"`
	Quantity  uint32    `gorm:"comment:购物车数量"`
	Remark    orm.JSON  `gorm:"comment:快照信息（名称、规格、单价、单位、图片、属性）;"`
	CreatedAt time.Time `gorm:"comment:添加时间"`
	UpdatedAt time.Time `gorm:"comment:更新时间"`
}

type OrderRequest struct {
	ID                uint64       `gorm:"comment:请求ID;type:bigserial;primaryKey"`
	BizNo             string       `gorm:"comment:请求ID;size:36;uniqueIndex:IDX_MALL_ORDER_REQUEST_BIZ_NO"`
	SgId              uint32       `gorm:"comment:品牌ID;type:integer;index:IDX_MALL_REQUEST_ORDER_SG,priority:1"`
	PStoreId          uint32       `gorm:"comment:总部&门店ID;type:integer;"`
	MemberId          uint64       `gorm:"comment:会员ID;type:bigint;index:IDX_MALL_ORDER_REQUEST_MEMBER,priority:1"`
	SubmitTime        time.Time    `gorm:"comment:下单时间;index:IDX_MALL_REQUEST_ORDER_SG,priority:2"`
	Trans             uint64       `gorm:"comment:应付总额;type:bigint"`
	CardInfo          orm.JSON     `gorm:"comment:卡信息"`
	AddressInfo       orm.JSON     `gorm:"comment:地址信息"`
	FreezeOrderNumber string       `gorm:"comment:冻结单号;size:32"`
	PayStatus         uint8        `gorm:"comment:支付状态，0~待支付，1~支付中，2~支付成功，3~支付失败，4~支付关闭"`
	PayOrderNumber    string       `gorm:"comment:支付单号;size:32"`
	PayRemark         orm.JSON     `gorm:"comment:支付订单信息"`
	InventoryStatus   bool         `gorm:"comment:履约单是否创建完毕"`
	AutoCancelTime    sql.NullTime `gorm:"comment:失效时间"`
	OrderCreated      bool         `gorm:"comment:订单是否创建完毕"`
	UpdatedAt         time.Time    `gorm:"comment:更新时间"`
}

type Order struct {
	ID              uint64       `gorm:"comment:订单ID;type:bigserial;primaryKey"`
	OrderNumber     string       `gorm:"comment:订单号;size:32;uniqueIndex:UNIQ_IDX_MALL_ORDER_NUMBER"`
	RequestId       uint64       `gorm:"comment:请求ID;type:bigint;index:IDX_MALL_ORDER_REQUEST_ID"`
	SgId            uint32       `gorm:"comment:品牌ID;type:integer;index:IDX_MALL_ORDER_SG,priority:1"`
	PStoreId        uint32       `gorm:"comment:总部&门店ID;type:integer;index:IDX_MALL_ORDER_SG,priority:2"`
	MemberId        uint64       `gorm:"comment:会员ID;type:bigint;index:IDX_MALL_ORDER_MEMBER,priority:1"`
	SubmitTime      time.Time    `gorm:"comment:下单时间;index:IDX_MALL_ORDER_SG,priority:3"`
	Remark          string       `gorm:"comment:订单备注;size:100"`
	Source          uint8        `gorm:"comment:下单平台，1~微信，2~支付宝，3~抖音;type:smallint"`
	ProdTotal       uint64       `gorm:"comment:商品总额;type:bigint"`
	DeliverFee      uint64       `gorm:"comment:配送费;type:bigint"`
	Discount        uint64       `gorm:"comment:促销金额;type:bigint"`
	Trans           uint64       `gorm:"comment:应付金额;type:bigint"`
	Refunded        uint64       `gorm:"comment:已审核通过退款金额;type:bigint"`
	Status          uint8        `gorm:"comment:订单状态，1~创建，2~交易成功；3~付款前关闭；4~付款后取消;type:smallint"`
	PromotionStatus bool         `gorm:"comment:明细是否分摊完毕"`
	WarehouseId     uint32       `gorm:"comment:发货仓库ID;type:integer"`
	DeliverType     uint8        `gorm:"comment:配送方式：0~自提，1~同城，2~异地，4~无需/商家配送;type:smallint"`
	BookingDate     sql.NullTime `gorm:"comment:预约发货日期"`
	InventoryStatus uint8        `gorm:"comment:0~未创建履约单，10~待备货，11~待自提，20~待发货，21~已发货待签收，98~已完成，99~已取消;type:smallint"`
	InventoryRemark orm.JSON     `gorm:"comment:仓库订单信息"`
	CompleteTime    sql.NullTime `gorm:"comment:订单完结时间(签收/取消)"`
	OperationRemark orm.JSON     `gorm:"comment:后台操作附加信息（操作人&拒绝原因）"`
	AutoConfirmTime sql.NullTime `gorm:"comment:自动确认时间"`
	RefundDeadline  sql.NullTime `gorm:"comment:售后截止时间"`
	UpdatedAt       time.Time    `gorm:"comment:更新时间"`
}

// 订单明细
type OrderDetail struct {
	ID               uint64    `gorm:"comment:明细ID;type:bigserial;primaryKey"`
	OrderId          uint64    `gorm:"comment:订单ID;type:bigint;index:IDX_MALL_ORDER_DETAIL_ORDER_ID"`
	MemberId         uint64    `gorm:"comment:会员ID;type:bigint;index:IDX_MALL_ORDER_DETAIL_MEMBER"`
	ProdId           uint64    `gorm:"comment:商品ID;type:bigint"`
	SkuId            uint64    `gorm:"comment:规格ID;type:bigint"`
	ProdName         string    `gorm:"comment:商品名称;size:100"`
	SkuName          string    `gorm:"comment:规格名称;size:100"`
	Pic              string    `gorm:"comment:主图快照;size:100"`
	Unit             string    `gorm:"comment:商品单位;size:32"`
	Price            uint64    `gorm:"comment:商品单价;type:bigint"`
	OriginPrice      uint64    `gorm:"comment:进货单价(供货组/商品库价格);type:bigint"`
	Quantity         uint64    `gorm:"comment:下单数量;type:bigint"`
	Weight           uint64    `gorm:"comment:下单重量(克);type:bigint"`
	Total            uint64    `gorm:"comment:下单总额;type:bigint"`
	Discount         uint64    `gorm:"comment:分摊金额;type:bigint"`
	Trans            uint64    `gorm:"comment:分摊后下单时应付/实付金额;type:bigint"`
	RefundedQuantity uint64    `gorm:"comment:已申请成功退数量(仅退款时为0);type:bigint"`
	RefundedTrans    uint64    `gorm:"comment:已申请成功退金额(允许仅退款，与数量不构成相关；允许退款金额修改，与单价也不构成相关);type:bigint"`
	IsRefunding      bool      `gorm:"comment:明细是否处于退款待审核状态"`
	UpdatedAt        time.Time `gorm:"comment:更新时间"`
}

type OrderDiscount struct {
	ID        uint64    `gorm:"comment:自增ID;type:bigserial;primaryKey"`
	OrderId   uint64    `gorm:"comment:订单ID;type:bigint;index:IDX_MALL_ORDER_DISCOUNT_ORDER_ID"`
	Name      string    `gorm:"comment:活动名称;size:32"`
	Discount  uint64    `gorm:"comment:活动金额"`
	Type      uint8     `gorm:"comment:0~促销活动，1~订单改价"`
	Remark    orm.JSON  `gorm:"comment:大字段，保存活动类型、id等信息"`
	CreatedAt time.Time `gorm:"comment:创建时间"`
}

// 售后订单（可能基于单号查询，故不建立order_id外键）
type Refund struct {
	ID              uint64       `gorm:"comment:申请ID;type:bigserial;primaryKey"`
	ApplyNumber     string       `gorm:"comment:申请号;size:32;uniqueIndex:UNIQ_IDX_MALL_REFUND_APPLY_NUMBER"`
	OrderId         uint64       `gorm:"comment:订单ID;type:bigint;index:IDX_MALL_REFUND_APPLY_ORDER_ID"`
	ApplyType       uint8        `gorm:"comment:申请类型，1~仅退款，2~退货退款，3~整单取消"`
	Status          uint8        `gorm:"comment:申请单状态，1~待审核，2~审核拒绝，3~待录入退货单号，4~退货入库中，5~交易完成，6~交易关闭"`
	SgId            uint32       `gorm:"comment:品牌ID;type:integer;index:IDX_MALL_REFUND_SG,priority:1"`
	PStoreId        uint32       `gorm:"comment:总部&门店ID;type:integer;index:IDX_MALL_REFUND_SG,priority:2"`
	MemberId        uint64       `gorm:"comment:会员ID;type:bigint;index:IDX_MALL_REFUND_MEMBER,priority:1"`
	ApplyTime       time.Time    `gorm:"comment:下单时间;index:IDX_MALL_REFUND_SG,priority:3"`
	Remark          orm.JSON     `gorm:"comment:退货申请信息（备注 & 整单取消原因）;size:100"`
	WarehouseId     uint32       `gorm:"comment:退货仓ID;type:integer"`
	WarehouseInfo   orm.JSON     `gorm:"comment:退货仓库快照信息"`
	Trans           uint64       `gorm:"comment:退款金额;type:bigint"`
	ReviewTime      sql.NullTime `gorm:"comment:审核时间"`
	ReviewRemark    orm.JSON     `gorm:"comment:审核信息（员工 & 拒绝原因）"`
	PayStatus       uint8        `gorm:"comment:支付状态，10~待提交退款，11~退款中；12~退款成功；13~退款协商成功；14~退款失败;type:smallint"`
	PayRemark       orm.JSON     `gorm:"comment:支付订单信息"`
	DeliverType     uint8        `gorm:"comment:配送方式：0~自提，1~同城，2~异地，4~无需/商家配送;type:smallint"`
	InventoryStatus uint8        `gorm:"comment:0~无需退货单，31~已发货待接收，32~已拒绝接收；33~退货完成；99~已取消退货;type:smallint"`
	UpdatedAt       time.Time    `gorm:"comment:更新时间"`
}

// 售后明细
type RefundDetail struct {
	ID        uint64    `gorm:"comment:明细ID;type:bigserial;primaryKey"`
	ApplyId   uint64    `gorm:"comment:申请ID;type:bigint;index:IDX_MALL_REFUND_DETAIL_APPLY_ID"`
	MemberId  uint64    `gorm:"comment:会员ID;type:bigint;index:IDX_MALL_REFUND_DETAIL_MEMBER"`
	OrderId   uint64    `gorm:"comment:原始订单ID;type:bigint;index:IDX_MALL_REFUND_DETAIL_ORDER_ID"`
	DetailId  uint64    `gorm:"comment:下单时明细ID;type:bigint;index:IDX_MALL_REFUND_DETAIL_REF_ID"`
	Reason    string    `gorm:"comment:退款原因（仅退款&退货退款模式）;size:100"`
	ProdId    uint64    `gorm:"comment:商品ID;type:bigint"`
	SkuId     uint64    `gorm:"comment:规格ID;type:bigint"`
	ProdName  string    `gorm:"comment:商品名称;size:100"`
	SkuName   string    `gorm:"comment:规格名称;size:100"`
	Pic       string    `gorm:"comment:主图快照;size:100"`
	Unit      string    `gorm:"comment:商品单位;size:32"`
	Price     uint64    `gorm:"comment:下单时单价;type:bigint"`
	Quantity  uint64    `gorm:"comment:申请数量（仅退款时可以为0）;type:bigint"`
	Trans     uint64    `gorm:"comment:申请金额，存在数量时小于单价*数量;type:bigint"`
	UpdatedAt time.Time `gorm:"comment:更新时间"`
}

// 执行成功后自动删除
type Schedule struct {
	ScheduleType uint8     `gorm:"comment:1~请求关闭,2~支付查询,3~履约创建,4~自动签收,5~退款创建;primaryKey"`
	OrderId      uint64    `gorm:"comment:对应自增ID;primaryKey"`
	ExecuteTime  time.Time `gorm:"comment:下一次重试时间;index:IDX_MALL_SCHEDULE"`
	Retry        uint32    `gorm:"comment:重试次数;type:integer;not null;default:0"`
	CreatedAt    time.Time `gorm:"comment:创建时间"`
	UpdatedAt    time.Time `gorm:"comment:更新时间"`
}

// 事务性消息
type NsqMessage struct {
	ID          uint64       `gorm:"comment:自增主键;primaryKey;type:bigserial;not null"`
	Topic       string       `gorm:"comment:topic;size:36;not null"`
	MsgKey      string       `gorm:"comment:消息唯一标识;size:36;not null"`
	Payload     orm.JSON     `gorm:"comment:消息内容"`
	Status      uint8        `gorm:"comment:消息状态， 0~初始，1~成功，2~标记成功;type:smallint;not null;default:0"`
	ExecuteTime sql.NullTime `gorm:"comment:下一次执行时间;index:IDX_MALL_NSQ_EXECUTE"`
	Retry       uint32       `gorm:"comment:重试次数;type:integer;not null;default:0"`
	CreatedAt   time.Time    `gorm:"comment:创建时间"`
	UpdatedAt   time.Time    `gorm:"comment:更新时间"`
}

// 装修模板
type DecorationTemplate struct {
	OperationMode uint8    `gorm:"comment:运营模式;primaryKey;type:smallint"`
	IsHQ          bool     `gorm:"comment:是否总部;primaryKey"`
	Decoration    orm.JSON `gorm:"comment:装修大字段"`
}
